from os import popen as figpo
import os
import sys

def figarrshell(c):
    global figsysteme
    try:
        figsysteme = 0
        sh = figpo(c)
        ps = sh.read().replace(chr(13) + chr(10), 
        chr(10)).replace(chr(13), chr(10)).split(chr(10))
        figsysteme = sh.close()
    except:
        print "error running arrshell command: " + chr(34) + str(c) + chr(34) 
    return ps[:]

try:
    if "--" in sys.argv[1:][0]:
        print "running", sys.argv[1:][0]
        command = sys.argv[1:][0].replace("--","").strip()
        print command
        if command == "find" :
            os.system("dir")

except :

    pass

while 1:
        command = raw_input().strip()
        if command == "quit": #to quit
            break;
        elif command[0:4].lower()== "find" and command.find("w/-") == -1 and command.find("w/o-") == -1 : # to find a file with a definite name or extension 
            first_index = command.lower().index("find") + 3
            output = "dir /b/a/s" + "\t" + command[first_index+1:].strip()
            findflag = 1
            dirs = figarrshell(output)
            for i in range(0, len(dirs)-1):
                 print dirs[i]
            

        elif command[0:4].lower() == "find" and command.find("w/-") != -1 :  # to find files with the name in it
            dirs = figarrshell("dir /a")
            first_index = command.lower().index("w/-") + 2
            with_name = command[first_index+1 : ].strip()
            for i in range (0,len(dirs)-1):
                if dirs[i].find(with_name) != -1 :
                    print dirs[i] + "\n"
          

        elif command[0:4].lower() == "find" and command.find("w/o-") != -1 : # to find files without the name in it
            dirs = figarrshell("dir /a")
            first_index = command.lower().index("w/o-") + 3
            without_name = command[first_index+1 : ].strip()
            for i in range (0,len(dirs)-1):
                if dirs[i].find(without_name) == -1 :
                    print dirs[i] + "\n"

        elif command[0:2].lower() == "dr" and command.find("-") != -1 : # to show the files/folders in subdirectories with in the mentioned directory
            dr_index = command.lower().index("dr") + 2
            rest = command[dr_index:].strip().replace("-" , "\\" )
            output = "dir" + "\t" + rest
            dirs = figarrshell(output)
            for i in range(0, len(dirs)-1):
                 print dirs[i]
           

              
            

        elif command.strip()== "cls"  : #clear screen
            output = "cls"
            dirs = os.system(output)
            dirs = ""

        elif command[0:2] != "dr" and command.find("dr") != -1 and command[0:1] != "l" : # to show the firsr n subdirectory/folders/files in a directory . for example : 6 dr Lib-email
            first_index = command.lower().index("dr") + 1
            output = "dir" + "\t" + command[first_index+1:].strip().replace("-","\\")
            dirs = figarrshell(output)
            number = int(command[0:first_index-1].strip())
            if number<len(dirs):
                for i in range(number):
                    print dirs[i+5] + "\n"
            else:
                for i in range (0,len(dirs)-1):
                    print dirs[i+5] + "\n"
            

        if command[0:2].lower() == "dr" and command.find("-") == -1 : #to show the  subdirectory/folders/files in a directory . for example : dr Lib
            dr_index = command.lower().index("dr") + 2
            rest = command[dr_index:].strip()
            output = "dir" + "\t" + rest
            dirs = figarrshell(output)
            for i in range(0, len(dirs)-1):
                 print dirs[i]
            

        if command[0:1]== "l" and command[0:2] != "dr" and command.find("dr") != -1 : #to show the last files 
            first_index = command.lower().index("dr") + 1
            output = "dir" + "\t" + command[first_index+1:].strip().replace("-","\\")
            dirs = figarrshell(output)
            number = int(command[1:first_index-1].strip())+ 1 # added 1 becuse theres an empty element at last
            new_dirs = dirs[-number:]
            for i in range (0, len(new_dirs)):
                print new_dirs[i]
            

        if command[0:3].lower()== "mkd" : # to create a folder
            first_index = command.lower().index("mkd")+2
            rest = command[first_index+1:].strip().replace("-" , "\\")
            output = "mkdir " + rest
            dirs = os.system(output)
            print "folder created"
            dirs= ""


        if command[0:4]== "echo" :
            first_index = command.index("echo")+ 3
            s = command[first_index+1:].strip().split()
            if s[1]== "-w" :
                output = "echo" + "\t" + s[0] + "\t" + ">" + "\t" + s[2]
            else :
                output = "echo" + "\t" + s[0] + "\t" + ">>" + "\t" + s[2]
                
            dirs = os.system(output)

         
            

       

        if command[0:4] == "copy" :
            first_index = command.index("copy") + 3
            try:
                s = command[first_index+1:].strip().replace("to"," ")
            except :
                print ("unexpected command")
            output = "copy" + "\t" + s 
            dirs =  os.system(output)
            print ("file copied ")


       

        elif command.strip() == "help":
            print "Keyword" + "\t\t\t\t              " + "Use"
            print "find [file name or extension] --> " +"         "+ "to find a file with a definite name or extension."
            print "dr [directory]-->" + "      " + "to show the or subdirectory/folders/files in a directory . for example : dr Lib"
            print "dr [directory]-[subdirectory]-->" + "       " + "to show the files/folders in subdirectories with in the mentioned directory"
            print "n dr [directory]-[subdirectory]--> " + "        " + "to show the firsr n subdirectory/folders/files in a directory . for example : 6 dr Lib-email"
            print "ln  [directory]-[subdirectory]-->" + "       " + "to show the firsr n subdirectory/folders/files in a directory from the last . for example : l6 dr Lib-email"
            print "find w/-[name]-->" + "      " + " to find files with the name in it"
            print "find w/o-[name]-->" + "      " + " to find files without the name in it"
            print "mkd [folder name]-->" + "     "+ "to create a folder"
            print "copy [file] to [folder]" + "      " + "copies a file to a folder"
            print "echo [text] [text file]" + "      " + "overrides a text to the text file"
            print  "echo-append [text] [text file]" + "      " + "adds a text to the text file"
           


    

            


   
        




